package com.sweetest.luffy.page;


import com.sweetest.luffy.constant.ResultCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

/**
 * 接口返回数据格式
 *
 * @author scott
 * @email jeecgos@163.com
 * @date 2019年1月19日
 */
@ApiModel(value = "接口返回对象", description = "接口返回对象")
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 返回处理消息
     */
    @ApiModelProperty(value = "返回处理消息")
    private String message = "操作成功！";

    /**
     * 返回代码
     */
    @ApiModelProperty(value = "返回代码")
    private Integer code = 0;

    @ApiModelProperty(value = "返回总数")
    private Long count = 0L;
    /**
     * 返回数据对象 data
     */
    @ApiModelProperty(value = "返回数据对象")
    private T data;

    /**
     * 时间戳
     */
    @ApiModelProperty(value = "时间戳")
    private long timestamp = System.currentTimeMillis();

    public Result() {

    }

    public static Result<Object> error500(String message) {
        Result<Object> r = new Result<Object>();
        r.setMessage(message);
        r.setCode(ResultCode.RES_CODE_ERROR);
        return r;
    }

    public Result<T> success(String message) {
        this.message = message;
        this.code = ResultCode.RES_CODE_OK;
        return this;
    }

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public Long getCount() {
        return count;
    }

    public Result<T> setCount(Long count) {
        this.count = count;
        return this;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public T getData() {
        return data;
    }

    public void setData(T result) {
        if (null == this.code || 0 == this.code) {
            this.code = ResultCode.RES_CODE_OK;
        }
        this.data = result;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    public static Result<Object> ok() {
        Result<Object> r = new Result<Object>();
//		r.setSuccess(true);
        r.setMessage("成功");
        r.setCode(ResultCode.RES_CODE_OK);
        return r;
    }

    public static Result<Object> ok(Integer code) {
        Result<Object> r = new Result<Object>();
        r.setMessage("成功");
        r.setCode(code);
        return r;
    }

    public static Result<Object> ok(String msg) {
        Result<Object> r = new Result<Object>();
        r.setMessage(msg);
        r.setCode(ResultCode.RES_CODE_OK);
        return r;
    }

    public static Result<Object> ok(Object data) {
        Result<Object> r = new Result<Object>();
        r.setData(data);
        r.setCode(ResultCode.RES_CODE_OK);
        return r;
    }

    public static Result<Object> error(String msg) {
        return error(ResultCode.RES_CODE_ERROR, msg);
    }

    public static Result<Object> error(Integer code, String msg) {
        Result<Object> r = new Result<Object>();
        r.setMessage(msg);
        r.setCode(code);
        return r;
    }

    /**
     * 访问未通过
     */
    public static Result<Object> noauth(String msg) {
        return error(ResultCode.RES_TOKEN_STATUS, msg);
    }
}
